package com.scm.web.controller.inventory;

import java.util.List;

import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.core.page.PageDomain;
import com.scm.common.core.page.TableSupport;
import com.scm.common.exception.ServiceException;
import com.scm.common.utils.DateUtils;
import com.scm.common.utils.StringUtils;
import com.scm.dto.req.inventory.SaveScmInventoryInReq;
import com.scm.dto.resp.invent.QueryInventoryInPageResp;
import com.scm.dto.resp.invent.ScmInventoryInDetailDo;
import com.scm.persist.model.ScmInventoryIn;
import com.scm.persist.model.ScmInventoryInDetail;
import com.scm.service.IScmInventoryInService;
import com.scm.web.controller.inventory.vo.ScmInventoryInProductDetailsVO;
import com.scm.web.controller.inventory.vo.ScmInventoryInVO;
import org.apache.commons.compress.utils.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.scm.common.annotation.Log;
import com.scm.common.enums.BusinessType;
import com.scm.common.core.controller.BaseController;
import com.scm.common.core.domain.AjaxResult;
import com.scm.common.utils.poi.ExcelUtil;
import com.scm.common.core.page.TableDataInfo;

/**
 * 库存-入库信息Controller
 * 
 * @author reptilemanager
 * @date 2024-03-17
 */
@Controller
@RequestMapping("/scm/inventory/in")
public class ScmInventoryInController extends BaseController
{
    private String prefix = "inventory/in";

    @Autowired
    private IScmInventoryInService scmInventoryInService;

    @RequiresPermissions("system:in:view")
    @GetMapping()
    public String in()
    {
        return prefix + "/in";
    }

    /**
     * 查询库存-入库信息列表
     */
    @RequiresPermissions("system:in:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScmInventoryIn scmInventoryIn)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer start = (pageDomain.getPageNum() - 1) * 10;
        QueryInventoryInPageResp resp = scmInventoryInService.selectScmInventoryInList(scmInventoryIn,start,pageDomain.getPageSize());
        return getDataTable(resp.getList());
    }

    /**
     * 导出库存-入库信息列表
     */
    @RequiresPermissions("system:in:export")
    @Log(title = "库存-入库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScmInventoryIn scmInventoryIn)
    {
        QueryInventoryInPageResp resp  = scmInventoryInService.selectScmInventoryInList(scmInventoryIn,0,10);
        ExcelUtil<ScmInventoryIn> util = new ExcelUtil<ScmInventoryIn>(ScmInventoryIn.class);
        return util.exportExcel(resp.getList(), "库存-入库信息数据");
    }

    /**
     * 新增库存-入库信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存-入库信息
     */
    @RequiresPermissions("system:in:add")
    @Log(title = "库存-入库信息", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult addOrUpdateSave(ScmInventoryInVO scmInventoryInVO)
    {
        SysUser sysUser = getSysUser();

        if(scmInventoryInVO==null){
            return toAjax(false);
        }

        if(scmInventoryInVO.getId()!=null&&scmInventoryInVO.getId()>0){
            scmInventoryInService.updateScmInventoryIn(getSaveScmInventoryInReq(scmInventoryInVO),sysUser);
        }else{
            scmInventoryInService.saveScmInventoryIn(getSaveScmInventoryInReq(scmInventoryInVO),sysUser);
        }
        return toAjax(1);
    }


    private SaveScmInventoryInReq getSaveScmInventoryInReq(ScmInventoryInVO scmInventoryInVO){

        SaveScmInventoryInReq req = new SaveScmInventoryInReq();
        ScmInventoryIn scmInventoryIn = new ScmInventoryIn();
        scmInventoryIn.setId(scmInventoryInVO.getId());
        scmInventoryIn.setCode(scmInventoryInVO.getCode());
        scmInventoryIn.setName(scmInventoryInVO.getName());
        scmInventoryIn.setSupplierName(scmInventoryInVO.getSupplierName());
        scmInventoryIn.setSupplierId(scmInventoryInVO.getSupplierId());
//        scmInventoryIn.setSalesmanId();
        scmInventoryIn.setSalesmanName(scmInventoryInVO.getSalesmanName());
//        scmInventoryIn.setPurchaseOrderId();
        scmInventoryIn.setRemark(scmInventoryInVO.getRemark());

        if(!StringUtils.isEmpty(scmInventoryInVO.getInDatetime())){
            scmInventoryIn.setInDatetimeStr(scmInventoryInVO.getInDatetime());
            scmInventoryIn.setInDatetime(DateUtils.parseDate(scmInventoryInVO.getInDatetime()).getTime());
        }
        req.setScmInventoryIn(scmInventoryIn);

        List<ScmInventoryInDetail> scmInventoryInDetailList = Lists.newArrayList();

        List<ScmInventoryInProductDetailsVO> details  = scmInventoryInVO.getProducts();

        for (ScmInventoryInProductDetailsVO detail : details) {
            ScmInventoryInDetail inventoryInDetail = new ScmInventoryInDetail();
            inventoryInDetail.setStashId(detail.getStashId());
            inventoryInDetail.setProductId(detail.getProductId());
            inventoryInDetail.setProductCode(detail.getProductCode());
            inventoryInDetail.setProductName(detail.getProductName());
            inventoryInDetail.setProductUnit(detail.getProductUnit());
//            inventoryInDetail.setProductSpec(detail.getProductSpec());
            inventoryInDetail.setProductCount(detail.getProductCount());
            inventoryInDetail.setInStashAmount(detail.getInStashAmount());
//            inventoryInDetail.setSettleAmount(detail.getSettleAmount());
            inventoryInDetail.setRemark(detail.getRemark());
            scmInventoryInDetailList.add(inventoryInDetail);
        }

        req.setScmInventoryInDetailList(scmInventoryInDetailList);
        return req;

    }
    /**
     * 修改库存-入库信息
     */
    @RequiresPermissions("system:in:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScmInventoryIn scmInventoryIn = scmInventoryInService.selectScmInventoryInById(id);

        if("beInStorage".equals(scmInventoryIn.getStatus())){
            throw new ServiceException("已入库单据不能修改！");
        }

        mmap.put("scmInventoryIn", scmInventoryIn);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存-入库信息
     */
    @RequiresPermissions("system:in:edit")
    @Log(title = "库存-入库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScmInventoryIn scmInventoryIn)
    {
        return toAjax(scmInventoryInService.updateScmInventoryIn(scmInventoryIn));
    }

    /**
     * 删除库存-入库信息
     */
    @RequiresPermissions("system:in:remove")
    @Log(title = "库存-入库信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        int i =  scmInventoryInService.deleteScmInventoryInById(ids);
        return toAjax(i);
    }


    /**
     * 查询库存-入库信息列表
     */
    @PostMapping("/detail/list")
    @ResponseBody
    public TableDataInfo detailList(@RequestParam("id") Long id, ModelMap mmap)
    {
        if(id==null||id==0){
            return getDataTable(Lists.newArrayList());
        }

        List<ScmInventoryInDetailDo> list = scmInventoryInService.selectScmInventoryInDetailListList(id);

        return getDataTable(list);
    }

    @PostMapping("/confirmScmInventoryIn/{id}")
    @ResponseBody
    public AjaxResult confirmScmInventoryIn(@PathVariable("id") Long id, ModelMap mmap)
    {
        try {
            if(id==null||id==0){
                return toAjax(false);
            }

            SysUser sysUser = getSysUser();

            scmInventoryInService.confirmScmInventoryIn(id,sysUser);

            return toAjax(true);
        } catch (Exception e) {
            return AjaxResult.error(""+e.getMessage());
        }


    }


}
